//
//  DES3Util.m
//
//

#import "DES3Util.h"
#import <CommonCrypto/CommonCryptor.h>
#import "VNDGTMBase64.h"

#define gkey			@"Security@freemode.cn#Developer_2014#"
#define gIv             @"0freemd0"

@implementation DES3Util

// jia'm
+ (NSString*)encrypt:(NSString*)plainText {
    NSData* data = [[self enOther:plainText] dataUsingEncoding:NSUTF8StringEncoding];
	size_t plainTextBufferSize = [data length];
	const void *vplainText = (const void *)[data bytes];
    
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;
    
    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    
    const void *vkey = (const void *) [gkey UTF8String];
    const void *vinitVec = (const void *) [gIv UTF8String];
    
    ccStatus = CCCrypt(kCCEncrypt,
                       kCCAlgorithm3DES,
                       kCCOptionPKCS7Padding,
                       vkey,
                       kCCKeySize3DES,
                       vinitVec,
                       vplainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);
    
    NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
	NSString *result = [VNDGTMBase64 stringByEncodingData:myData];
    return result;
}

// Ω‚√‹∑Ω∑®
+ (NSString*)decrypt:(NSString*)encryptText {
    NSData *encryptData = [VNDGTMBase64 decodeData:[[self deOther:encryptText] dataUsingEncoding:NSUTF8StringEncoding]];
	size_t plainTextBufferSize = [encryptData length];
	const void *vplainText = [encryptData bytes];
    
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;
    
    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    
    const void *vkey = (const void *) [gkey UTF8String];
    const void *vinitVec = (const void *) [gIv UTF8String];
    
    ccStatus = CCCrypt(kCCDecrypt,
                       kCCAlgorithm3DES,
                       kCCOptionPKCS7Padding,
                       vkey,
                       kCCKeySize3DES,
                       vinitVec,
                       vplainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);
    
    NSString *result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr 
								length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding];
    return result;
}

+(NSString*)enOther:(NSString*)string{
    NSString *stringReplace = [[string stringByReplacingOccurrencesOfString:@"[+]" withString:@"-XX-"] stringByReplacingOccurrencesOfString:@" " withString:@"-X-"];
    return stringReplace;
}

+(NSString*)deOther:(NSString*)string{
    NSString *stringReplace = [[string stringByReplacingOccurrencesOfString:@"-XX-" withString:@"+"] stringByReplacingOccurrencesOfString:@"-X-" withString:@" "];
    return stringReplace;
}

@end